{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    def mySqrt(self, x: int) -> int:\n",
    "        left = 0\n",
    "        right = x\n",
    "        sqrt = 0\n",
    "        while left <= right:\n",
    "            mid = (left + right) // 2\n",
    "            if mid*mid == x:\n",
    "                sqrt = mid\n",
    "                break\n",
    "            elif mid*mid < x:\n",
    "                left = mid + 1\n",
    "                sqrt = mid\n",
    "            else:\n",
    "                right = mid - 1\n",
    "        return sqrt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = Solution()\n",
    "s.mySqrt(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py3",
   "language": "python",
   "name": "py3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
